# -*- coding: utf-8 -*-
# Copyright (c) Facebook, Inc. and its affiliates.

"""
Common data processing utilities that are used in a
typical object detection data pipeline.
"""

from detectron2.data.detection_utils import convert_PIL_to_numpy
from detectron2.utils.file_io import PathManager
from PIL import Image


def read_dianjiao_image(file_name, format=None, convert=None):
    """
    Read an image into the given format.
    Will apply rotation and flipping if the image has such exif information.

    Args:
        file_name (str): image file path
        format (str): one of the supported image modes in PIL, or "BGR" or "YUV-BT.601".

    Returns:
        image (np.ndarray):
            an HWC image in the given format, which is 0-255, uint8 for
            supported image modes in PIL or "BGR"; float (0-1 for Y) for YUV-BT.601.
    """
    with PathManager.open(file_name, "rb") as f:
        image = Image.open(f)
        w, h = image.size
        if h > w:
            image = image.transpose(Image.ROTATE_90)    # 逆时针旋转90°
        return convert_PIL_to_numpy(image, format)